//getUserInfo.js
//获取应用实例
const app = getApp()

Page({
  data: {
  },

  getUserPhone (e) {
    console.log(e)
    if (e.detail.encryptedData) {
      this.getUserPhoneFormAPI(e.detail)
      app.globalData.phoneCode = e.detail
    }
  },
  getUserPhoneFormAPI(detail) {
    const requestData = {
      encrypteddata: detail.encryptedData,
      iv: detail.iv,
      wmptoken: app.globalData.wmptoken
    }
    app.api.weChatBinding(requestData).then(res => {
      console.log(res)
      if (res.resultCode === 0) {
        wx.setStorageSync('token', res.data.token)
        wx.setStorageSync('uid', res.data.uid)
        app.globalData.isLogin = true
        wx.navigateBack()
      }
      // {
      //   "data": {
      //     "uid": null,
      //       "token": "8dd169c0bfd50a70",
      //       "wmptoken": null,
      //       "openid": null,
      //       "sessionkey": null
      //   },
      //   "result": true,
      //     "resultCode": 0,
      //     "msg": "Success"
      // }
      if (res.resultCode === 3002) {
        wx.showModal({
          title: '未找到当前手机号绑定信息',
          content: '是否前去注册',
          confirm: '注册',
          cancel: '暂不登录',
          success (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/login/login'
              })
            }
          }
        })

      }
      // if (res.code === 0) {
      //   wx.setStorageSync('token', res.data.token)
      //   app.globalData.isLogin = true
      //   wx.navigateBack()
      // }
    })
  },
  navToAgreementPage () {
    wx.navigateTo({
      url: '/pages/agreement/agreement'
    })
  }
})
